;(function($, window, document, undefined) {

    // Create the defaults once
    var pluginName = "MiniCounter",
        defaults = {
            tpl:    '<i class="mc-num-days"><%=days %></i><span class="mc-text">天</span>' +
                    '<i class="mc-num-hours"><%=hours %></i><span class="mc-text">小时</span>' +
                    '<i class="mc-num-minutes"><%=minutes %></i><span class="mc-text">分</span>' +
                    '<i class="mc-num-seconds"><%=seconds %></i><span class="mc-text">秒</span>',
            timeTo: null,
            countDown: null,
            callback: null
        };

    // The actual plugin constructor
    function Plugin(element, options) {
        var self = this;

        self.el = element;
        self.$el = $(element);

        self.settings = $.extend({}, defaults, options);
        self._defaults = defaults;
        self.timeTo = self.settings.timeTo || new Date(self.$el.data('time-to')) || new Date();
        self.countDown = self.settings.countDown || parseInt(self.$el.data('countdown'), 10) || 0;

        self._name = pluginName;
        self.init();
    }

    Plugin.prototype = {
        init: function() {
            var self = this;

            self.start();
            console.log("xD");
        },
        start: function() {
            var self = this;

            var totalMS = 0;
            if (self.countDown > 0) {
                totalMS = self.countDown * 1000;
            } else {
                var target = self.timeTo;
                var targetTime = target.getTime(),
                    nowTime = new Date().getTime(),
                    totalMS = targetTime - nowTime;
            }
            if (totalMS > 0) {
                self.timeExact(totalMS);
            }
        },
        stop: function() {
            var self = this;

            window.clearTimeout(self.timer);
            self.timer = null;
        },
        updateCounter: function(obj) {
            var self = this;

            // var tpl = self.settings.tpl;
            var html = '<i class="mc-num-minutes">' + obj.minutes + '</i><span class="mc-text">分</span>' +
                       '<i class="mc-num-seconds">' + obj.seconds + '</i><span class="mc-text">秒</span>';

            if (obj.days > 0) {
                html = '<i class="mc-num-days">' + obj.days + '</i><span class="mc-text">天</span>' +
                       '<i class="mc-num-hours">' + obj.hours + '</i><span class="mc-text">小时</span>' + html;
            } else {
                if (obj.hours > 0) {
                    html = '<i class="mc-num-hours">' + obj.hours + '</i><span class="mc-text">小时</span>' + html;
                }
            }

            self.$el.html(html);
        },
        timeExact: function(totalMS) {
            var self = this;

            var msPerDay = 24 * 60 * 60 * 1000; // 定义一天有多少秒

            // 当前时间和倒计时时间之间的毫秒差
            var msOldTotal = totalMS;

            // 将毫秒差转换为秒，以及取整
            var sOldTotal = msOldTotal / 1000;

            var eDaysOld = msOldTotal / msPerDay,   // 将秒转换为天
                daysOld = Math.floor(eDaysOld);     // 取整 天数

            var eHoursOld = (eDaysOld - daysOld) * 24,  // 取不足一天的时间转换为小时数
                hoursOld = Math.floor(eHoursOld);       // 取整 小时

            var eMinutesOld = (eHoursOld - hoursOld) * 60,  // 取不足一小时的时间转换为分
                minutesOld = Math.floor(eMinutesOld);       // 取整 分

            var eSecondsOld = (eMinutesOld - minutesOld) * 60,  // 取不足一小时的时间转换为秒
                secondsOld = Math.floor(eSecondsOld);           // 取整数 秒

            var left = {
                days: daysOld,
                hours: hoursOld,
                minutes: minutesOld,
                seconds: secondsOld
            };
            var clk = self.settings.callback;
            if (daysOld < 0) {

                if (clk && typeof clk === 'function') {
                    clk(left);
                }

            } else {

                self.updateCounter(left);
                self.timer = window.setTimeout(function() {
                    self.timeExact(msOldTotal - 200);
                }, 200);

            }
        }
    };

    // A really lightweight plugin wrapper around the constructor,
    // preventing against multiple instantiations
    $.fn[pluginName] = function(options) {
        return this.each(function() {
            if (!$(this).data("plugin_" + pluginName)) {
                $(this).data("plugin_" + pluginName, new Plugin(this, options));
            }
        });
    };

})($, window, document);
